package com.compton.UI
{
	import com.compton.VO.WineVO;
	
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	
	import libs.CloseButton;
	import libs.DetailMask;
	import libs.DetailView;
	import libs.LeftEmpty;
	import libs.LeftRating;
	import libs.MinButton;
	import libs.RightEmpty;
	import libs.RightRating;
	
	public class WineDetails extends DetailView 
	{
		private var _details:WineVO;
		private var _detailMask:DetailMask;
		private var _imagesVO:WineVO = new WineVO();
		
		public function WineDetails()
		{
			super();
			winBg.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
			buttons();
		}
		
		//created the minimize and close buttons for the window
		private function buttons():void
		{
			var closeBtn:CloseButton = new CloseButton();
			addChild(closeBtn);
			closeBtn.x = this.width - 30;
			closeBtn.y = 5;
			closeBtn.buttonMode = true;
			closeBtn.addEventListener(MouseEvent.CLICK, onClose);
			
			var minBtn:MinButton = new MinButton();
			addChild(minBtn);
			minBtn.x = closeBtn.x - 25;
			minBtn.y = closeBtn.y;
			minBtn.buttonMode = true;
			minBtn.addEventListener(MouseEvent.CLICK, onMin);
			
			this.buyNow.addEventListener(MouseEvent.CLICK, onBuy);
			this.buyNow.buttonMode = true;
		}
		
		//directs the customer to the website to buy the wine available
		private function onBuy(evt:MouseEvent):void
		{
			navigateToURL(new URLRequest(_details.link));
		}
		
		//allows window to me minimized
		private function onMin(evt:MouseEvent):void
		{
			stage.nativeWindow.minimize();
		}
		
		//allows the window to be closed
		private function onClose(evt:MouseEvent):void
		{
			stage.nativeWindow.close();
		}
		
		//allows the window to be dragged
		private function onDown(evt:MouseEvent):void
		{
			stage.nativeWindow.startMove();
		}

		/**
		 * Getter and Setter for receiving information from the thumbnail that was clicked. Then setting this information inside
		 * the detailed wine page window that is displayed. 
		 * @return returns the information recieved from the thumbnail pressed.
		 */		
		public function get details():WineVO
		{
			return _details;
		}

		/** 
		 * @private
		 */		
		public function set details(value:WineVO):void
		{
			_details = value;
			updateUI();
			loadImage();
		}
		
		//updates the text fields inside the window created with updated information. Also encodes the text to be read in Flash
		private function updateUI():void
		{
			tfType.htmlText = HtmlText.convertHTMLEntities(_details.type);
			tfType.mouseEnabled = false;
			tfWine.htmlText = HtmlText.convertHTMLEntities(_details.name);
			tfWine.mouseEnabled = false;
			tfWinery.htmlText = HtmlText.convertHTMLEntities(_details.winery);
			tfWinery.mouseEnabled = false;
			tfYear.text = _details.vintage;
			tfYear.mouseEnabled = false;
			tfPrice.text = _details.price;
			tfPrice.mouseEnabled = false;
			winBg.detailContainer.mouseEnabled = false;
			
			for(var i:int = 0; i < _details.ranking * 2 ; i++)
			{	
				if(i%2 > 0)
				{	
					var image2:RightRating = new RightRating();
					image2.x = i*(image.width) + 132; 
					image2.y = image.y;
					this.addChild(image2);
					
				}else{
					var image:LeftRating = new LeftRating();
					image.x = i*(image.width) + 132;
					image.y = image.y + 130;
					this.addChild(image);
				}
			}
			
			for(var p:int = 0; p < 10; p++)
			{ 
				if(p%2 > 0)
				{
					var image3:RightEmpty = new RightEmpty();
					image3.x = p*(image4.width) + 132;
					image3.y = image3.y + 130;
					this.addChildAt(image3,4);
				}else{
					var image4:LeftEmpty = new LeftEmpty();
					image4.x = p*(image4.width) + 132;
					image4.y = image4.y + 130;
					this.addChildAt(image4,4);
				}
			}
		}
		
		//loades the image that was passed in and places it onto the detail viewing window
		private function loadImage():void
		{	
			_detailMask = new DetailMask();
			_detailMask.x = LargeImage.x;
			_detailMask.y = LargeImage.y;
			LargeImage.mask = _detailMask;
			
			var imageLoader:Loader = new Loader();
			imageLoader.load(new URLRequest(_details.image));
			imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
		}
		
		//on image loaded will paste the image to the new window filled with wine details
		private function onImageLoaded(evt:Event):void
		{
			var image:Bitmap = evt.currentTarget.content;
			image.x = -(image.width - _detailMask.width)/2;
			LargeImage.addChild(image);
			LargeImage.width = image.width;
			LargeImage.height = image.height;
		}
	}
}